package com.zhubayi.shirodb.entity;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhubayi.shirodb.config.MyByteSource;
import com.zhubayi.shirodb.service.UserService;
import com.zhubayi.shirodb.utils.ApplicationContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhubayi
 */
@Slf4j
public class MyRealm extends AuthorizingRealm {
    private UserService userService;
    public MyRealm(UserService userService){
        this.userService=userService;
    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //拿到主体
        String primaryPrincipal =(String) principalCollection.getPrimaryPrincipal();
        //得到用户信息包含权限权限信息
        User user = userService.findRolesByUserName(primaryPrincipal);
        //授权信息
        if(!CollectionUtils.isEmpty(user.getRoles())){
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            user.getRoles().forEach(role -> {
                //角色信息
                simpleAuthorizationInfo.addRole(role.getName());
                //权限信息
                List<Perms> perms = userService.findPermsByRoleId(role.getId());
                // 功能列表
                Set<String> menus = new HashSet();
                if(!CollectionUtils.isEmpty(perms)){
                    for (Perms permission : perms) {
                        if(StringUtils.isNotEmpty(permission.getName())) {  //加一个非空判断
                            menus.add(permission.getName());
                        }
                    }
                    simpleAuthorizationInfo.addStringPermissions(menus);
                }
            });
            return simpleAuthorizationInfo;
        }
        return null;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取身份信息
        String username=(String) authenticationToken.getPrincipal();
        //得到service
        //得到用户信息
        User user=userService.findByUserName(username);
        if(!ObjectUtils.isEmpty(user)){
            //返回数据库信息
            return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),
                    new MyByteSource(user.getSalt()),this.getName());
        }
        return null;
    }
}
